package besta.moon.sql.expression.function.string;

import besta.moon.sql.expression.function.FunctionReturnP1StringAndBinary;
import besta.moon.sql.parser.SQLTokenizer;

public class FunctionLTrim extends FunctionReturnP1StringAndBinary {

    @Override
    public final int getFunction() {
        return SQLTokenizer.LTRIM;
    }

    @Override
    public final boolean isNull() throws Exception {
        return param1.isNull();
    }

    @Override
    public final byte[] getBytes() throws Exception {
        if (isNull()) {
            return null;
        }
        byte[] bytes = param1.getBytes();
        int start = 0;
        int length = bytes.length;
        while (start < length && bytes[start] == 0) {
            start++;
        }
        length -= start;
        byte[] b = new byte[length];
        System.arraycopy(bytes, start, b, 0, length);
        return b;
    }

    @Override
    public final String getString() throws Exception {
        if (isNull()) {
            return null;
        }
        String str = param1.getString();
        int start = 0;
        while (start < str.length() && str.charAt(start) == ' ') {
            start++;
        }
        return str.substring(start);
    }

}
